package com.yunhe.util;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


public class SqlSessionFactoryUtil {
	//定义私有静态SqlSessionFactory
	private static SqlSessionFactory sqlSessionFactory=null;
	//类线程锁用来保证多线程访问时的安全机制
	//私有化构造器,避免使用者使用new创建多个对象
	private SqlSessionFactoryUtil(){
		
	}
	//构建SqlSessionFactory
	public static SqlSessionFactory initSqlSessionFactory(){
		String resource = "mybatis-config.xml";
		InputStream inputStream =null;
		try{
			inputStream=Resources.getResourceAsStream(resource);
			
		}catch(IOException ex){
			ex.printStackTrace();
		}
		
		sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream);
		return sqlSessionFactory;

			
	}
	public static SqlSession openSqlSession(){
		initSqlSessionFactory();
		
			return  sqlSessionFactory.openSession();
				
	}
				

}
